package cn.com.shopec.erp.product.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.commo.BrandVoConvertor;
import cn.com.shopec.erp.product.commo.ProductServKeyGenerator;
import cn.com.shopec.erp.product.dao.IBrandDao;
import cn.com.shopec.erp.product.entity.Brand;
import cn.com.shopec.erp.product.entity.CatBrand;
import cn.com.shopec.erp.product.service.IBrandService;
import cn.com.shopec.erp.product.service.ICatBrandService;
import cn.com.shopec.erp.product.vo.BrandVo;

/**
 * 
 * @ClassName: BrandServiceImpl
 * @Description: 品牌业务service接口实现类
 * @author: duhui
 * @date 2013-5-27 上午09:58:48
 * 
 */
@Service
public class BrandServiceImpl implements IBrandService {

    @Resource
    private IBrandDao brandDao;

    @Resource
    private BrandVoConvertor brandVoConvertor;
    
    @Resource
    private ICatBrandService catBrandService;

    @Override
    @Transactional
    public void addBrand(BrandVo brandVo) throws Exception {
        Brand brand = this.brandVoConvertor.convert2Brand(brandVo);

        if (LogicUtil.isNotNull(brand)) {
            brand.setBrandNo(ProductServKeyGenerator.getBrandNo());
            brand.setCreateTime(DateUtil.formatTimesTampDate(new Date()));
        }

        Brand br = this.brandDao.save(brand);
        //分类添加
        if(LogicUtil.isNotNull(brandVo.getCatNos())){
        	for (int i = 0; i < brandVo.getCatNos().length; i++) {
        		CatBrand cb = new CatBrand();
            	cb.setCatNo(brandVo.getCatNos()[i]);
            	cb.setBrandNo(br.getBrandNo());
            	catBrandService.AddCatBrand(cb);
			}
        }

    }

    @Override
    @Transactional
    public void updateBrand(BrandVo brandVo) throws Exception {
        if (LogicUtil.isNotNull(brandVo)) {
            Brand brand = this.brandDao.get("brandNo", brandVo.getBrandNo());
            this.brandDao.update(this.brandVoConvertor.convert2Brand(brandVo, brand));
            
            if(LogicUtil.isNotNull(brandVo.getCatNos())){
	            //删除之前的品牌分类
	            CatBrand catb = new CatBrand();
	            catb.setBrandNo(brand.getBrandNo());
	            this.catBrandService.delectCatBrand(catb);
	            
	            //新增品牌分类
	            for (int i = 0; i < brandVo.getCatNos().length; i++) {
            		CatBrand cb = new CatBrand();
    	            cb.setBrandNo(brandVo.getBrandNo());
    	            cb.setCatNo(brandVo.getCatNos()[i]);
    	            this.catBrandService.AddCatBrand(cb);
				}
	            
            }
        }

    }

    @Override
    @Transactional
    public void delBrand(String brandNo) throws Exception {
        if (LogicUtil.isNotNullAndEmpty(brandNo)) {
            Brand brand = this.brandDao.get("brandNo", brandNo);
            if (LogicUtil.isNotNull(brand)) {
                brand.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);
                CatBrand cb = new CatBrand();
                cb.setBrandNo(brandNo);
                this.catBrandService.delectCatBrand(cb);
                this.brandDao.update(brand);
            }

        }
    }

    @Override
    public BrandVo getBrand(String brandNo) {

        return this.brandVoConvertor.convert2BrandVo(this.brandDao.get("brandNo", brandNo));
    }
    
    @Override
    public BrandVo getBrandName(String brandName) {

        return this.brandVoConvertor.convert2BrandVo(this.brandDao.get("brandName", brandName));
    }

    @Override
    public PageFinder<BrandVo> getPagedBrandList(BrandVo brandVo, PageQuery pageQuery) throws Exception {
        DetachedCriteria dc = this.brandDao.createCriteria();

        if (LogicUtil.isNotNull(brandVo)) {
            if (LogicUtil.isNotNullAndEmpty(brandVo.getBrandName())) {
                dc.add(Restrictions.like("brandName", "%" + brandVo.getBrandName().trim() + "%"));
            }
        }
        dc.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
        // 按添加时间倒序
        dc.addOrder(Order.desc("createTime"));

        PageFinder<Brand> pagedBrandList = this.brandDao.pagedByCriteria(dc, pageQuery.getPage(),
                pageQuery.getPageSize());

        return this.brandVoConvertor.convert2PagedBrandVos(pagedBrandList);
    }

    @Override
    public List<BrandVo> getBrandList() throws Exception {

        return this.brandVoConvertor.convert2BrandVos(this.brandDao.getAll());
    }

    @Override
    public List<BrandVo> getBrandListByNo(String[] brandNo) {

        if (LogicUtil.isNotNull(brandNo)) {
            DetachedCriteria dc = this.brandDao.createCriteria();
            dc.add(Restrictions.in("brandNo", brandNo));
            dc.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
            return this.brandVoConvertor.convert2BrandVos(this.brandDao.findByCriteria(dc));
        }
        return null;
    }

    @Override
    public String validationRepeatNo(BrandVo brandVo) {
        DetachedCriteria criteria = this.brandDao.createCriteria();
        if(LogicUtil.isNotNull(brandVo)){
        	if (StringUtil.isExist(brandVo.getNo())) {
                criteria.add(Restrictions.eq("no", brandVo.getNo()));
            }
        	if (StringUtil.isExist(brandVo.getBrandName())) {
                criteria.add(Restrictions.eq("brandName", brandVo.getBrandName()));
            }
        	criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
        	List<Brand> catList = this.brandDao.findByCriteria(criteria);
            if (catList.size() > 0) {
                return SZSCMConstant.SUCCESS;
            } else {
                return SZSCMConstant.FAIL;
            }
        }
        return SZSCMConstant.FAIL;
    }

    
	@Override
	public Brand getBrandByBrandCode(String brandNo) {
		
		return brandDao.findUniqueBy("brandNo", brandNo);
	}
	
	@Override
	public Brand getBrandByNo(String brandNo) {
		
		return brandDao.findUniqueBy("no", brandNo);
	}
	
}
